#include <iostream>
#include <sstream>

#include <ipc_client.h>
#include <logger.h>

int main()
{
    auto client = IpcClient::GetInstance();
    auto ret = client->Connect("127.0.0.1", 55555);
    if (ret) {
        std::cerr << "Connect failed with " << ret << std::endl;
        return 1;
    }

    while (true) {
        std::cout << "Input a number: ";
        int id = 0;
        std::cin >> id;
        if (std::cin.eof()) {
            break;
        }

        IpcMessageParcel parcel, reply;
        parcel.way = IpcMessageParcel::Way::sync;
        parcel.callingSid = 123;
        {
            std::stringstream ss;
            ss << "get " << id;
            parcel.data = ss.str();
        }
        auto ret = IpcClient::GetInstance()->Send(parcel);
        if (ret <= 0) {
            std::cerr << "Send" << LOGSYSERR << std::endl;
            break;
        }

        ret = IpcClient::GetInstance()->Recv(reply);
        if (ret <= 0) {
            std::cerr << "Recv" << LOGSYSERR << std::endl;
            break;
        }

        std::cout << "return: " << reply.data << std::endl;
    }

    client->Disconnect();
    return 0;
}
